package com.kanesoft.android.gdx.control;

import com.badlogic.gdx.Gdx;
import com.kanesoft.android.gdx.actors.fight.AutoBehaviorFightLevelEasy;
import com.kanesoft.android.gdx.actors.fight.ControlActorFight;
import com.kanesoft.android.gdx.actors.fight.ControlActorFightKeyboard;
import com.kanesoft.android.gdx.actors.fight.ControlActorFightKeyboardP2;
import com.kanesoft.android.gdx.actors.fight.ControlActorFightTouch;
import com.kanesoft.android.gdx.actors.fight.FightActorObject;
import com.kanesoft.android.gdx.widgets.WidgetEvents;
import com.kanesoft.android.gdx.widgets.sprites.SpriteInfo;
import com.kanesoft.android.gdx.widgets.sprites.SpriteSlideInfo;
import com.kanesoft.android.gdx.widgets.sprites.SpriteWidget;

public class FightControl implements WidgetEvents{
    

    private FightActorObject fighterLeft;
    private FightActorObject fighterRight;
    
    private SpriteWidget powerBarBackground;
    private SpriteWidget powerBarActorLeft;
    private SpriteWidget powerBarActorRight;
    
    private SpriteWidget fightSplashMessage;
    
    private AutoBehaviorFightLevelEasy autoFightLeft;
    private AutoBehaviorFightLevelEasy autoFightRight;

    private ControlActorFight controlActor;
    private boolean autoPlayMode;
    
    private boolean invertCpuUpdateOrder;
    private ControlActorFightTouch touchHandler;
    

    private float resizePowerbarFactor = 1;

    public FightControl(FightActorObject fighterLeft, FightActorObject fighterRight, boolean autoPlayMode) {
        this.fighterLeft = fighterLeft;
        this.fighterRight = fighterRight;
        this.autoPlayMode = autoPlayMode;
        
        autoFightLeft = new AutoBehaviorFightLevelEasy(fighterLeft, fighterRight);
        autoFightRight = new AutoBehaviorFightLevelEasy(fighterRight, fighterLeft);

        controlActor = new ControlActorFight();
        
        touchHandler = new ControlActorFightTouch(fighterLeft);

        controlActor.addInputProcessor(new ControlActorFightKeyboard(fighterLeft));
        controlActor.addInputProcessor(new ControlActorFightKeyboardP2(fighterRight));
        controlActor.addInputProcessor(touchHandler);
        

        
        float powerbarWidth = 858;
        SpriteInfo spiBackgroundPowerbar = new SpriteInfo("data/power_bar_display.png", 0.100f);
        spiBackgroundPowerbar.addSpriteSlide(new SpriteSlideInfo(0, 0, (int) powerbarWidth, 47));
        
        if (powerbarWidth > (Gdx.graphics.getWidth() + 20)) {
            resizePowerbarFactor = (Gdx.graphics.getWidth() - 20)/powerbarWidth;
        }
        
        float powerWidth = powerbarWidth*resizePowerbarFactor;
        powerBarBackground = new SpriteWidget((Gdx.graphics.getWidth()/2) - (powerWidth/2), 425, resizePowerbarFactor, resizePowerbarFactor, spiBackgroundPowerbar, null);
        
        SpriteInfo spiPowerbar = new SpriteInfo(spiBackgroundPowerbar, 0.100f);
        spiPowerbar.addSpriteSlide(new SpriteSlideInfo(3, 50, 1, 82));
        
        
        powerBarActorLeft = new SpriteWidget(55, 380, 0, resizePowerbarFactor, spiPowerbar, null);
        powerBarActorRight = new SpriteWidget(520, 380, 0, resizePowerbarFactor, spiPowerbar, null);
        
        
        float fightTxtWidth = 579;
        float resizeFightTxtFactor = 1;
        if (fightTxtWidth > (Gdx.graphics.getWidth() + 200)) {
            resizeFightTxtFactor = (Gdx.graphics.getWidth() - 100)/fightTxtWidth;
        }
        
        fightTxtWidth = fightTxtWidth*resizePowerbarFactor;
        
        SpriteInfo spiFightSplashMessage = new SpriteInfo("data/fight_text.png", 1f);
        spiFightSplashMessage.addSpriteSlide(new SpriteSlideInfo(10, 10, 1, 1));
        spiFightSplashMessage.addSpriteSlide(new SpriteSlideInfo(0, 0, 579, 428));
        spiFightSplashMessage.addSpriteSlide(new SpriteSlideInfo(10, 10, 1, 1));

        fightSplashMessage = new SpriteWidget((Gdx.graphics.getWidth()/2)-(fightTxtWidth/2), Gdx.graphics.getHeight()/6, resizeFightTxtFactor, resizeFightTxtFactor, spiFightSplashMessage, this);
        fightSplashMessage.setAnimationInLoop(false);
        
    }
    
    private void updateFigtersSpriteFlip() {

        if (fighterLeft.getPosition().x < fighterRight.getPosition().x) {
            fighterRight.setHorizontalFlip(true);
            fighterLeft.setHorizontalFlip(false);
        } else {
            fighterRight.setHorizontalFlip(false);
            fighterLeft.setHorizontalFlip(true);
        }
    }
    
    public void updateFightControl() {
        
        touchHandler.checkForPitchAngle();
        
        updateFigtersSpriteFlip() ;
        
        if (autoPlayMode) {

            if (invertCpuUpdateOrder) {
                autoFightRight.updateCpuActions(5f);
                autoFightLeft.updateCpuActions(6f);
            } else {
                autoFightLeft.updateCpuActions(3f);
                autoFightRight.updateCpuActions(4f);
            }
            invertCpuUpdateOrder = !invertCpuUpdateOrder;
        } else {
            //autoFightRight.updateCpuActions(5f);
        }
        
        powerBarBackground.draw(Gdx.graphics.getDeltaTime(),0,0);
        float maxPowerbarSize = 385 * resizePowerbarFactor;

        powerBarActorLeft.setSpriteSize(maxPowerbarSize - ((((float) fighterLeft.getLifePower()) / 100) * maxPowerbarSize), 1);
        powerBarActorLeft.draw(Gdx.graphics.getDeltaTime(),0,0);
        powerBarActorRight.setSpriteSize(maxPowerbarSize - ((((float) fighterRight.getLifePower()) / 100) * maxPowerbarSize), 1);
        powerBarActorRight.setSpritePosition(520 + maxPowerbarSize - (maxPowerbarSize - ((((float) fighterRight.getLifePower()) / 100) * maxPowerbarSize)),
                powerBarActorRight.getPoxY());
        powerBarActorRight.draw(Gdx.graphics.getDeltaTime(),0,0);
        
        fightSplashMessage.draw(Gdx.graphics.getDeltaTime(),0,0);
        
        touchHandler.drawTouchArrows(Gdx.graphics.getDeltaTime());
    }

    @Override
    public void onClick() {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationComplete() {
        fightSplashMessage.enabled = false;
        fightSplashMessage.destroyWidget();
        this.fighterLeft.startFight();
        this.fighterRight.startFight();
    }

    @Override
    public void onSpriteOutOfScreen() {
        // TODO Auto-generated method stub
    }

    @Override
    public void actualAttackBehavior(boolean attackBehavior) {
        // TODO Auto-generated method stub
    }

}
